package es.efor.ics.gev.model;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Embeddable;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Version;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

@Entity
@Table(name = "ORGANIZACIONES_EVENTOS")
public class OrganizacionEvento extends BaseObject implements Serializable {

	/**
	 * Embedded composite identifier class that represents the primary key
	 * columns of the many-to-many join table.
	 */
	@Embeddable
	public static class Id implements Serializable {
		
		private static final long serialVersionUID = 7908174953553375422L;
		
		private Long eventoId;
		private Long perfilId;
		private Long organizacionId;		

		public Id() {
		}
		
		public Id(Long eventoId, Long perfilId, Long organizacionId) {
			this.eventoId = eventoId;
			this.perfilId = perfilId;
			this.organizacionId = organizacionId;
		}

		@Column(name = "EVENTO_ID")
		public Long getEventoId() {
			return eventoId;
		}
		
		public void setEventoId(Long eventoId) {
			this.eventoId = eventoId;
		}

		@Column(name = "PERFIL_ID")
		public Long getPerfilId() {
			return perfilId;
		}

		public void setPerfilId(Long perfilId) {
			this.perfilId = perfilId;
		}

		@Column(name = "ORGANIZACION_ID")
		public Long getOrganizacionId() {
			return organizacionId;
		}

		public void setOrganizacionId(Long organizacionId) {
			this.organizacionId = organizacionId;
		}
		
		@Override
		public boolean equals(Object o) {
			if (o instanceof Id) {
				Id that = (Id) o;
				return this.eventoId.equals(that.eventoId)
						&& this.perfilId.equals(that.perfilId)
						&& this.organizacionId.equals(that.organizacionId);
			} else {
				return false;
			}
		}

		@Override
		public int hashCode() {
			return eventoId.hashCode() + perfilId.hashCode()
					+ organizacionId.hashCode();
		}

		@Override
		public String toString() {
			return new ToStringBuilder(this, ToStringStyle.DEFAULT_STYLE)
					.append("eventoId", this.getEventoId()).append("perfilId",
							this.getPerfilId()).append("organizacionId",
							this.getOrganizacionId()).toString();
		}
		
	}
	
	// static data
	private static final long serialVersionUID = -2008872666160713222L;

	// private instance data
	private Id id;
	private Evento evento;
	private Perfil perfil;
	private Organizacion organizacion;
	private Integer version;
	
	protected OrganizacionEvento(){		
	}
	
	public OrganizacionEvento(Evento evento, Perfil perfil,
			Organizacion organizacion) {		
		this.evento = evento;
		this.perfil = perfil;
		this.organizacion = organizacion;
		
		// Set primary key
		this.id = new Id(evento.getId(), perfil.getId(), organizacion.getId());
	}

	// ********************** Accessor Methods ********************** //
	
	@EmbeddedId
	public Id getId() {
		return id;
	}

	public void setId(Id id) {
		this.id = id;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "EVENTO_ID", insertable = false, updatable = false)
	@org.hibernate.annotations.ForeignKey(name = "FK_ORG_EVENTO_EVENTO_ID")
	public Evento getEvento() {
		return evento;
	}

	public void setEvento(Evento evento) {
		this.evento = evento;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "PERFIL_ID", insertable = false, updatable = false)
	@org.hibernate.annotations.ForeignKey(name = "FK_ORG_EVENTO_PERFIL_ID")
	public Perfil getPerfil() {
		return perfil;
	}

	public void setPerfil(Perfil perfil) {
		this.perfil = perfil;
	}
	
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "ORGANIZACION_ID", insertable = false, updatable = false)
	@org.hibernate.annotations.ForeignKey(name = "FK_ORG_EVENTO_ORG_ID")	
	public Organizacion getOrganizacion() {
		return organizacion;
	}

	public void setOrganizacion(Organizacion organizacion) {
		this.organizacion = organizacion;
	}
	
	@Version
	@Column(name = "OBJ_VERSION")
	public Integer getVersion() {
		return version;
	}

	public void setVersion(Integer version) {
		this.version = version;
	}
	
	// ********************** Common Methods ********************** //
	
	@Override
	public boolean equals(Object o) {
		if (this == o)
			return true;
		if (o == null || getClass() != o.getClass())
			return false;
		final OrganizacionEvento that = (OrganizacionEvento) o;
		return id.equals(that.id);
	}

	@Override
	public int hashCode() {
		return id.hashCode();
	}

	@Override
	public String toString() {
		return new ToStringBuilder(this, ToStringStyle.DEFAULT_STYLE).append(
				"evento", this.getEvento()).append("perfil", this.getPerfil())
				.append("organizacion", this.getOrganizacion()).toString();
	}

}
